package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.control.AddressControl;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.control.bean.AddressData;
import com.dongdongsongcan.app.control.bean.AreaData;
import com.su.dolphin.control.bean.BaseBean;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.DpTitleBar;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

public class AddressEditActivity extends BaseActivity {

    private TextView mAddressedit_name;
    private RadioGroup mAddressedit_gender;
    private TextView mAddressedit_louhao;
    private TextView mAddressedit_mobile;
    private TextView mAddressedit_address_distance;
    private View mAddressedit_btn;
    private int mMode;
    public static final int MODE_ADD = 0;
    public static final int MODE_EDIT = 1;
    private TextView mAddressedit_area;
    private AddressData.Address mAddress;
    private AreaData.Area mArea;


    public static void startFrom(Context context, AddressData.Address address) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("address", address);
        DpActivityManager.i()
                .startActivity(context, AddressEditActivity.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mAddress = (AddressData.Address) this.getIntent().getExtras().get("address");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_edit);

    }

    @Override
    public void initView() {
        mAddressedit_area = (TextView) findViewById(R.id.addressedit_area);
        mAddressedit_name = (TextView) findViewById(R.id.addressedit_name);
        mAddressedit_gender = (RadioGroup) findViewById(R.id.addressedit_gender);
        mAddressedit_louhao = (TextView) findViewById(R.id.addressedit_louhao);
        mAddressedit_mobile = (TextView) findViewById(R.id.addressedit_mobile);
        mAddressedit_address_distance = (TextView) findViewById(R.id.addressedit_address_distance);
        mAddressedit_btn = findViewById(R.id.addressedit_btn);
    }

    @Override
    public void setListener() {
        mAddressedit_area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AreaChooseActivity.startFrom(AddressEditActivity.this);
            }
        });

        mAddressedit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddressControl.i().user_address_del(mAddress.getInfo_id(), new CtrlTask.IControlResponse<BaseBean>() {
                    @Override
                    public void onControlResponse(BaseBean data) {
                        if (data.isOk()){
                            TipsUtil.toast(data.getMsg());
                            finish();
                        }
                    }
                });
            }
        });
    }


    @Override
    public void initData() {
        if (mAddress != null) {
            mMode = MODE_EDIT;
            mAddressedit_area.setText(mAddress.getInfo_area());
            mAddressedit_name.setText(mAddress.getInfo_name());
            mAddressedit_louhao.setText(mAddress.getInfo_louhao1());
            mAddressedit_mobile.setText(mAddress.getInfo_mobile());
            if (TextUtils.equals(mAddress.getInfo_sex(), "先生")) {
                mAddressedit_gender.check(R.id.addressedit_male);
            } else if (TextUtils.equals(mAddress.getInfo_sex(), "小姐")) {
                mAddressedit_gender.check(R.id.addressedit_female);
            }
            mAddressedit_btn.setVisibility(View.VISIBLE);
        } else {
            mMode = MODE_ADD;
            mAddressedit_btn.setVisibility(View.GONE);
        }

        getDpTitleBar().setRightButtonText("保存", new DpTitleBar.OnRightButtonClickListener() {
            @Override
            public void onRightBtnClick(View button) {
                if (!checkInputValid()) return;

                String name = mAddressedit_name.getText().toString();
                String louhao = mAddressedit_louhao.getText().toString();
                String mobile = mAddressedit_mobile.getText().toString();
                String area = mAddressedit_area.getText().toString();
                String sex = mAddressedit_gender.getCheckedRadioButtonId() == R.id.addressedit_male ? "先生" :
                        "小姐";

                AddressData.Address address = new AddressData.Address();
                address.setInfo_name(name);
                address.setInfo_louhao1(louhao);
                address.setInfo_mobile(mobile);
                address.setInfo_sex(sex);

                if (mMode == MODE_ADD) {
                    insertAddress(address);
                } else {
                    editAddress(address);
                }
            }
        }, null, null);
    }

    //编辑,使用旧地址
    private void editAddress(AddressData.Address address) {
        mAddress.setInfo_name(address.getInfo_name());
        mAddress.setInfo_louhao1(address.getInfo_louhao1());
        mAddress.setInfo_mobile(address.getInfo_mobile());
        mAddress.setInfo_sex(address.getInfo_sex());

        if (mArea != null) {
            mAddress.setInfo_area(mArea.getArea_id());
            mAddress.setSite_x(mArea.getSite_x());
            mAddress.setSite_y(mArea.getSite_y());
        }

        AddressControl.i().user_address_update(mAddress, new CtrlTask.IControlResponse<BaseBean>() {
            @Override
            public void onControlResponse(BaseBean data) {
                TipsUtil.toast(data.getMsg());
            }
        });
        finish();
    }


    private void insertAddress(AddressData.Address address) {
        if (mArea == null) {
            TipsUtil.toast("请选择配送区域");
            return;
        }
        address.setInfo_area(mArea.getArea_id());
        address.setSite_x(mArea.getSite_x());
        address.setSite_y(mArea.getSite_y());
        AddressControl.i().user_address_insert(address, new CtrlTask.IControlResponse<BaseBean>() {
            @Override
            public void onControlResponse(BaseBean data) {
                TipsUtil.toast(data.getMsg());
            }
        });
        finish();
    }

    private boolean checkEditText(TextView editText) {
        boolean result = true;
        if (editText == null) {
            result = false;
        }
        String text = editText.getText().toString();
        if (TextUtils.isEmpty(text)) {
            result = false;
        }
        return result;
    }

    private boolean checkInputValid() {
        if (!checkEditText(mAddressedit_name)) {
            TipsUtil.toast("请输入姓名");
            return false;
        }
        if (!checkEditText(mAddressedit_louhao)) {
            TipsUtil.toast("请输入具体位置");
            return false;
        }
        if (!checkEditText(mAddressedit_mobile)) {
            TipsUtil.toast("请输入手机号");
            return false;
        }
        return true;
    }

    public void setArea(AreaData.Area address) {
        if (address != null) {
            mArea = address;
            mAddressedit_area.setText(mArea.getArea_name());
        }
    }
}
